package com.sctu.EMSApp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.adapter.PlayFoodMoreListviewAdapter;
import com.sctu.bean.PlayFoodList;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

/**
 * Created by Administrator on 2015/12/13.
 */
public class PlayFoodMoreListActivity extends Activity {
    private ImageView returnfragment;
    //头部的title
    private TextView headtitleitext;
    private ListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.food_category_more);
        initview();
    }

    private void initview() {
        //获取listview
        listView = (ListView) findViewById(R.id.food_more_list);

        //对返回按钮监控，进行返回
        returnfragment = (ImageView) findViewById(R.id.food_category_more_return_img);
        returnfragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PlayFoodMoreListActivity.this.finish();
            }
        });

        String uuid = getIntent().getStringExtra("uuid");
        GetVolleyHttp.getInstance(this).getStringGETJsonData(UrlConfig.URL + "/category/" + uuid + "/articles", listener, errorlistener);

    }

    // 当网络请求返回成功（200的状态）
    Response.Listener<String> listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            Log.e("Json", json);
            // 开始解析JSON
            // 反序列化，直接将JSON反序列化为Test这个Class对象
            final PlayFoodList results = JSON.parseObject(json,
                    PlayFoodList.class);
            listView.setAdapter(new PlayFoodMoreListviewAdapter(PlayFoodMoreListActivity.this, results));

            //监听listitem
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent();
                    intent.setClass(PlayFoodMoreListActivity.this,PalyFoodOneDetailsActivity.class);
                    intent.putExtra("id",results.getResults().get(i).getId());
                    startActivity(intent);
                }
            });
            //返回上一级
            returnfragment = (ImageView) findViewById(R.id.food_category_more_return_img);
            returnfragment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    PlayFoodMoreListActivity.this.finish();
                }
            });
            //对头部的title进行设置
            headtitleitext = (TextView) findViewById(R.id.food_category_more_title_txt);
            headtitleitext.setText(results.getResults().get(0).getCategory());



        }
    };

    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(PlayFoodMoreListActivity.this, "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();
        }
    };

}
